import re

#uraj = 356
#chrysalis = 216 + 228 = 444
#khalis 219 + 228 = 447
#khaydarin = 357
w = open('heal_trig.txt', 'r')
r = open('regen_trig2.txt', 'r')
u = open('shield_trig.txt', 'r')

htemplate = w.read()
rtemplate = r.read()
stemplate = u.read()

w.close()
r.close()
u.close()

#regex
GET_PERCENT = re.compile(r'(?<= )[0-9][0-9](?=%|,)')
GET_DC = re.compile(r'(?<=Exactly, )[0-9]+(?=\))')
GET_DC2 = re.compile(r'(?<=- )[0-9]+(?= -)')
GET_DC3 = re.compile(r'(?<=- )[0-9]+(?= DCs)')
GET_DC4 = re.compile(r'(?<=Set To, )[0-9]+(?=\))')
GET_EXACTLY = re.compile(r'Exactly')

def heal_macro(percent):
    w = open('heal_trigs.txt', 'w')
    #calculate the number of iterations needed
    iterations = ((100 - int(GET_PERCENT.findall(htemplate)[0])) / percent) + 1
    for x in range(0, iterations):
        new_str = htemplate
        increase = (x * percent) + int(GET_PERCENT.findall(new_str)[0])
        new_str = GET_PERCENT.sub(str(increase), new_str)
        new_str = GET_DC.sub(str(x + 1), new_str)
        new_str = GET_DC2.sub(str(x + 1), new_str)
        if x == iterations - 1:
            new_str = GET_EXACTLY.sub('At least', new_str)
        print>>w, new_str
    w.close()


def shield_macro(percent):
    w = open('shield_trigs.txt', 'w')
    #calculate the number of iterations needed
    iterations = ((100 - int(GET_PERCENT.findall(htemplate)[0])) / percent) + 1
    for x in range(0, iterations + 1):
        new_str = stemplate
        increase = (x * percent) + int(GET_PERCENT.findall(new_str)[0])
        new_str = GET_PERCENT.sub(str(increase), new_str)
        new_str = GET_DC.sub(str(x + 1), new_str)
        new_str = GET_DC2.sub(str(x + 1), new_str)
        if x == iterations - 1:
            new_str = GET_EXACTLY.sub('At least', new_str)
        print>>w, new_str
    w.close()



def regen_macro():
    w = open('regen_trigs.txt', 'w')
    #iterations
    iterations = GET_DC3.findall(rtemplate)[0]
    total = 0
    for x in range(int(iterations), 0, -1):
        new_str = rtemplate
        new_str = GET_DC.sub(str(total + 1), new_str)
        new_str = GET_DC3.sub(str(x), new_str)
        new_str = GET_DC4.sub(str(x), new_str)
        new_str = GET_DC2.sub(str(total + 1), new_str)
        if x == 1:
            new_str = GET_EXACTLY.sub('At least', new_str)
        total += 1
        print>>w, new_str
    w.close()



def regen_macro2(max_regen):
    w = open('regen_trigs2.txt', 'w')
    for x in range(1, max_regen):
        new_str = rtemplate
        p = re.compile(r'(?<=At least, )0')
        new_str = p.sub(str(x), new_str)
        print>>w, new_str
    w.close()


    
